using System;
using UnityEngine;

public class Node : MonoBehaviour
{
    public static Action<Node> OnNodeSelected; // 当节点被选中时的事件
    public static Action OnTurretSold;

    public Turret Turret { get; set; } // 节点上的炮塔

    [SerializeField] private GameObject attackRangeSprite;//攻击范围显示
    private float _rangesize;
    private Vector3 _rangeOriginalsize;

    private void Start()
    {
        _rangesize = attackRangeSprite.GetComponent<SpriteRenderer>().bounds.size.y;
        _rangeOriginalsize = attackRangeSprite.transform.localScale;
    }

    public void CloseAttackRangeSprite(){
        attackRangeSprite.SetActive(false);
    }

    private void ShowTurretInfo()
    {
        attackRangeSprite.SetActive(true);
        attackRangeSprite.transform.localScale = _rangeOriginalsize * Turret.attackRange / (_rangesize / 2);
    }

    public void SetTurret(Turret turret)
    {
        Turret = turret; // 设置节点上的炮塔
    }

    public bool IsEmpty()
    {
        return Turret == null; // 判断节点是否为空（没有炮塔）
    }

    public void SelectTurret()
    {
        OnNodeSelected?.Invoke(this); // 触发节点被选中事件
        if (!IsEmpty())
        {
            ShowTurretInfo();
        }
    }

    public void SellTurret()
    {
        if (!IsEmpty())
        {
            CurrencySystem.Instance.AddCoins(Turret.TurretUpgrade.GetsellValue());
            Destroy(Turret.gameObject);
            Turret = null;
            attackRangeSprite.SetActive(false);
            OnTurretSold?.Invoke();
        }
    }
}
